package com.wolfsea.windowmanagerdemo
import android.animation.ObjectAnimator
import android.content.Context
import android.os.Handler
import android.os.Looper
import android.util.AttributeSet
import android.view.WindowManager
import android.view.animation.LinearInterpolator
import android.widget.FrameLayout
import kotlinx.android.synthetic.main.popwindow_set_wallpaper.view.*

/**
 *@desc  窗口视图
 *@author liuliheng
 *@time 2022/4/16  23:53
 **/
class WindowView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null,
    defaultResId: Int = 0
) : FrameLayout(context, attributeSet, defaultResId) {

    private var onlyOnce = true
    private val mEnterInterpolator = LinearInterpolator()

    private lateinit var mEnterAnimator: ObjectAnimator

    private var onDismiss: (() -> Unit)? = null
    private var onShow: (() -> Unit)? = null

    init {
        inflate(context, R.layout.popwindow_set_wallpaper, this)
    }

    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        onShow?.invoke()
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        if (onlyOnce) {
            mEnterAnimator =
                ObjectAnimator.ofFloat(
                    this@WindowView,
                    "translationY",
                    -this@WindowView.measuredHeight.toFloat(),
                    0F
                )
            mEnterAnimator.interpolator = mEnterInterpolator
            mEnterAnimator.duration = ANIMATION_DURATION
            mEnterAnimator.start()

            onlyOnce = false
        }
    }

    fun show(onShow: () -> Unit) {
        this.onShow = onShow
    }

    fun dismiss(onDismiss: () -> Unit) {
        this.onDismiss = onDismiss
    }

    fun hide(removeNow: Boolean = false) {
        if (!this@WindowView.isAttachedToWindow) {
            return
        }
        val windowManager =
            this.context.getSystemService(Context.WINDOW_SERVICE) as? WindowManager ?: return
        if (removeNow) {
            if (this@WindowView.isAttachedToWindow) {
                onDismiss?.invoke()
                windowManager.removeViewImmediate(this@WindowView)
            }
            return
        }

        ll_popup_content_layout.isClickable = false

        val anim = ObjectAnimator.ofFloat(
            this@WindowView,
            "translationY",
            0F,
            -this@WindowView.measuredHeight.toFloat()
        )
        anim.interpolator = mEnterInterpolator
        anim.duration = ANIMATION_DURATION
        anim.start()

        Handler(Looper.getMainLooper()).postDelayed({
            if (this@WindowView.isAttachedToWindow) {
                onDismiss?.invoke()
                windowManager.removeViewImmediate(this@WindowView)
            }
        }, ANIMATION_DURATION)
    }

    companion object {
        const val ANIMATION_DURATION: Long = 500
    }

}